package com.hkts.naturalgas.performance.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson;
import com.hkts.naturalgas.performance.entity.HkAssessmentPicture;
import com.hkts.naturalgas.performance.mapper.HkAssessmentPictureMapper;
import com.hkts.naturalgas.performance.performanceVo.PerformanceScoreListVo;
import com.hkts.naturalgas.performance.service.IHkAssessmentPictureService;
import com.hkts.naturalgas.utils.IDUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-04-08
 */
@Service
@Transactional
public class HkAssessmentPictureServiceImpl extends ServiceImpl<HkAssessmentPictureMapper, HkAssessmentPicture> implements IHkAssessmentPictureService {


@Autowired
private HkAssessmentPictureMapper hkAssessmentPictureMapper;

@Override
public IPage<HkAssessmentPicture> selectPage(Page<HkAssessmentPicture> page, HkAssessmentPicture hkAssessmentPicture){
        QueryWrapper<HkAssessmentPicture> query = Wrappers.query();
        QueryWrapper<HkAssessmentPicture> entityWrapper = getEntityWrapper(query, hkAssessmentPicture);
        return hkAssessmentPictureMapper.selectPage(page, entityWrapper);
        }


@Override
public List<HkAssessmentPicture> selectList(HkAssessmentPicture hkAssessmentPicture){
        QueryWrapper<HkAssessmentPicture> query = Wrappers.query();
        QueryWrapper<HkAssessmentPicture> entityWrapper = getEntityWrapper(query, hkAssessmentPicture);
        return hkAssessmentPictureMapper.selectList(entityWrapper);
        }

/**
 *  公共查询条件
 * @param entityWrapper
 * @return
 */
public QueryWrapper<HkAssessmentPicture> getEntityWrapper(QueryWrapper<HkAssessmentPicture> entityWrapper,HkAssessmentPicture hkAssessmentPicture){
        //条件拼接
                if(StringUtils.isNotBlank(hkAssessmentPicture.getId())){
                entityWrapper.like(HkAssessmentPicture.ID,hkAssessmentPicture.getId());
                }
                if(StringUtils.isNotBlank(hkAssessmentPicture.getProcessInstanceId())){
                entityWrapper.like(HkAssessmentPicture.PROCESS_INSTANCE_ID,hkAssessmentPicture.getProcessInstanceId());
                }
                if(StringUtils.isNotBlank(hkAssessmentPicture.getIndexId())){
                entityWrapper.like(HkAssessmentPicture.INDEX_ID,hkAssessmentPicture.getIndexId());
                }

        return entityWrapper;
        }
    @Override
    public void saveImageByProcessAndItem(String processId, String indexId, List imageList) {
       for(int i=0;i<imageList.size();i++){
           HkAssessmentPicture picture=new HkAssessmentPicture();
           picture.setId(IDUtils.genImageName());
           picture.setIndexId(indexId);
           picture.setPictureOrder(0+1);
           picture.setProcessInstanceId(processId);
           picture.setPictureUrl(imageList.get(i).toString());
           hkAssessmentPictureMapper.insert(picture);
       }
    }
    /*
    * 提交并修改
    * */
    @Override
    public void updateImageByProcessAndItem(String processId, String indexId, List imageList) {
        PerformanceScoreListVo vo=new PerformanceScoreListVo();
        vo.setIndexId(indexId);
        vo.setProcessInstanceId(processId);
        hkAssessmentPictureMapper.deletPicturesByProcessAndIndex(vo);
        for(int i=0;i<imageList.size();i++){
            HkAssessmentPicture picture=new HkAssessmentPicture();
            picture.setId(IDUtils.genImageName());
            picture.setIndexId(indexId);
            picture.setPictureOrder(0+1);
            picture.setProcessInstanceId(processId);
            picture.setPictureUrl(imageList.get(i).toString());
            hkAssessmentPictureMapper.insert(picture);
        }
    }

}
